/**
 * 
 */
package br.gov.sjc.socialalimenta.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import br.gov.sjc.classes.Documentos;
import br.gov.sjc.classes.Endereco;

/**
 * @author rodrigo.ramos
 *
 */
public class EnderecoDAO extends SQLiteOpenHelper {
	private static final String TABELA = "tab_SocialA_Endereco";
	private static final String DATABASE_NAME = "SocialAlimenta";
	private static final int VERSION = 1;
	private static final int DATABASE_VERSION = 1;
	private static final String[] COLS = {"_id", "SiasN", "Endereco", "numero","Ap","Bloco","Complemento","Referencia","CEP","Bairro"};
	
	public EnderecoDAO(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "CREATE TABLE IF NOT EXISTS " + TABELA + 
		"(_id INTEGER PRIMARY KEY autoincrement," +
		" SiasN INTEGER," + 
		" Endereco TEXT," +
		" numero TEXT," + 
		" Ap TEXT, " +
		" Bloco TEXT, " +
		" Complemento TEXT, " +
		" Referencia TEXT, " +
		" CEP TEXT, " +
		" Bairro TEXT " +
		");";
		db.execSQL(sql);/**/
		Log.i("EnderecoDAO","onCreate");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		 db.execSQL("DROP TABLE IF EXISTS " + EnderecoDAO.TABELA);
		 this.onOpen(db);
		
		Log.i("EnderecoDAO","onUpgrade");
	}
	
	public void inserir(Endereco end){ 
		ContentValues valores = new ContentValues();
		valores.put("SiasN", end.getSiasN());
		valores.put("Endereco", end.getEndereco());
		valores.put("numero", end.getNumero());
		valores.put("Ap", end.getAp());
		valores.put("Bloco", end.getBloco());
		valores.put("Complemento", end.getComplemento());
		valores.put("Referencia", end.getReferencia());
		valores.put("CEP", end.getCEP());
		valores.put("Bairro", end.getBairro());
		
		getWritableDatabase().insert(TABELA, null, valores);
	}
	
	public List<Endereco> getLista(){
		Cursor c = getWritableDatabase().query(TABELA, COLS, null, null, null, null, null);
		List<Endereco> lista = new ArrayList<Endereco>();
		while(c.moveToNext()){
			Endereco end = new Endereco();
			end.set_id(c.getInt(0));
			end.setSiasN(c.getInt(1));
			end.setEndereco(c.getString(2));
			end.setNumero(c.getString(3));
			end.setAp(c.getString(4));
			end.setBloco(c.getString(5));
			end.setComplemento(c.getString(6));
			end.setReferencia(c.getString(7));
			end.setCEP(c.getString(8));
			end.setBairro(c.getString(9));
			
			lista.add(end);
		}
		c.close();
		
		return lista;
		
	}

	public Endereco getById(int posicao) {
		Cursor c = getWritableDatabase().query(TABELA, COLS, "_id=?", new String[]{"" + posicao}, null, null, null);
		c.moveToFirst();
		
		Endereco end = new Endereco();
		end.set_id(c.getInt(0));
		end.setSiasN(c.getInt(1));
		end.setEndereco(c.getString(2));
		end.setNumero(c.getString(3));
		end.setAp(c.getString(4));
		end.setBloco(c.getString(5));
		end.setComplemento(c.getString(6));
		end.setReferencia(c.getString(7));
		end.setCEP(c.getString(8));
		end.setBairro(c.getString(9));
		
		c.close();
		return end;
	}

	public void alterar(Endereco end) {
		ContentValues valores = new ContentValues();
		valores.put("SiasN", end.getSiasN());
		valores.put("Endereco", end.getEndereco());
		valores.put("numero", end.getNumero());
		valores.put("Ap", end.getAp());
		valores.put("Bloco", end.getBloco());
		valores.put("Complemento", end.getComplemento());
		valores.put("Referencia", end.getReferencia());
		valores.put("CEP", end.getCEP());
		valores.put("Bairro", end.getBairro());
		
		getWritableDatabase().update(TABELA, valores, "_id=?", new String[]{"" + end.get_id()});
		
	}

	public boolean recreateTable(SQLiteDatabase database) {
		boolean retorno = false;
		try {
			database.execSQL("DROP TABLE IF EXISTS " + TABELA);
	    	onCreate(database);
	    	retorno = true;
		}catch(Exception e){
			Log.e(TABELA,"ERRO MENSAGEM:"+e.getMessage());
        }
		return retorno;
	}

}
